ProgressBar= function() {
	
    $.fn.progressTimer = function (options) {
		var settings = $.extend({}, $.fn.progressTimer.defaults, options);
		
	
		
        this.each(function () {
            var barContainer = $("<div>");
            var bar = $("<div id='progressDiv'>").addClass("progress-bar").addClass(settings.completeStyle)
                .attr("role", "progressbar")
                .attr("aria-valuenow", "0")
                .attr("aria-valuemin", "0")
                .attr("aria-valuemax", settings.timeLimit);
			barContainer.appendTo($(this));			
			bar.appendTo(barContainer) ;		  
          	document.getElementById('progressDiv').style.height='50px';            
            var start = new Date();
            var limit = settings.timeLimit * 1000;
            
             interval = window.setInterval(function ()
            {
                var elapsed = new Date() - start;				
                if((elapsed / limit) * 100>100){
            		bar.width("100%");
            	}else{
            		bar.width(((elapsed / limit) * 100) + "%");   	
            	}
				//la progression devient rouge à partir de la condition				
                if (limit - elapsed <=5000){
                	bar.removeClass(settings.baseStyle)
                       .removeClass(settings.completeStyle)
                       .addClass(settings.warningStyle);
					// change le background en rouge de la div
					document.getElementById('progressTimer').style.backgroundColor='#910909';
					//Animation des chiffres
					$('#Seconde').addClass("SecondeShake");
						 
				}				
				// Décompte
				var CurrentTime = parseInt(((elapsed / limit) * 30));				
				$('#Seconde').html(settings.timeLimit-CurrentTime);		 
				if ($('#Seconde').html()=="0"){
					$('#Seconde').html('Temps écoulé !');
				}
				// fin de la pregress bar				
				if (elapsed >= limit){
                    window.clearInterval(interval);
					//le décompte est terminé : on retire l'animation des secondes
					$('#Seconde').removeClass("SecondeShake");	
                    settings.onFinish.call(this);
                    //Affiche la bonne réponse et effectue une redirection vers la page principale.
                    goodAnswer();
                }
            }, 250);
        });
        
    	// au click d'une réponse :
    	$(".answer").click(function (){                		
    		// fin de linterval	
    		stop();
    		settings.onFinish.call(this);		
        	$(".answer").off();
        	$(".answer").removeClass('answerhover');
     		$(".answer").css('cursor', 'default');      	
            $('#Seconde').removeClass("SecondeShake");
            $("#progressDiv").css('width','100%');
            //récupère l'id du boutton sur lequel luser clique
            clickId=$(this).attr("id");
            //Détermine la reaction a dorpter en fonction de la répone
            goodAnswer(clickId);
    	});
        return this;
    };	
	var stop=function(){
		window.clearInterval(interval);
	};
    $.fn.progressTimer.defaults = {
        timeLimit: 60,  //total number of seconds
        warningThreshold: 5,  //seconds remaining triggering switch to warning color
        onFinish: function () {},  //invoked once the timer expires
		baseStyle: '',  //bootstrap progress bar style at the beginning of the timer
        warningStyle: 'progress-bar-danger',  //bootstrap progress bar style in the warning phase
        completeStyle: 'progress-bar-success'  //bootstrap progress bar style at completion of timer
		
    };
	$("#progressTimer").progressTimer({
		timeLimit: 30,		 
	});
	
	function goodAnswer(answerId){
		var anyAnswer=$('.answer').eq(0).attr('id').replace("answer","");
		var idPartie=$('#idPartie').val();
		var idDomaine=$('#idDomaine').val();
     	$.ajax({
     		url : "/trivia/CReponse/goodAnswerJS/" + anyAnswer,
     		context : document.body
 		}).done(function(data) {
 			$(".answer").off();
			$(".answer").removeClass('answerhover');
			$(".answer").css('cursor', 'default');
			var goodAnswer="answer"+data.trim();
			document.getElementById(goodAnswer).style.backgroundColor='#73E95C';
 			if(answerId!=undefined){            			
				if(answerId==goodAnswer){
					document.getElementById('progressDiv').style.backgroundColor='#73E95C';
					document.getElementById("Seconde").innerHTML = "Bonne réponse !";
					RedirectionDomaine();         				 
				}else{
					document.getElementById('progressDiv').style.backgroundColor='#d9534f';
					document.getElementById(answerId).style.backgroundColor='#d9534f';
					document.getElementById("Seconde").innerHTML = "Mauvaise réponse !";
					RedirectionPartie();
				}
 			}else{
 				$.ajax({
 					url : "/trivia/CQuestion/changePlayer/"+idPartie+"/"+idDomaine,
 					context : document.body
 				}).done(function(data) {
 					RedirectionPartie();
 				}); 				
 			}     			
 		});
	}
	
	function RedirectionPartie(){		
		setTimeout(function() {
			$.ajax({
				url : "/trivia/CPartie/",
				context : document.body
			}).done(function(data) {
				$(".bloc_page").html(data);
			});
		}, 3000);
	}
	
	function RedirectionDomaine(){
		setTimeout(function() {
			$.ajax({
				url : "/trivia/CDomaine/",
				type : "POST",
				data : "idpartie="+$("#idPartie").val(),
				context : document.body
			}).done(function(data) {
				$(".bloc_page").html(data);
			});
		}, 5000);
	}
}
